package appapi.service.impl;

import appapi.Request.ParkCarPageReq;
import appapi.Vo.brider.AddCar;
import appapi.Vo.brider.CarVO;
import appapi.Vo.brider.EditCar;
import appapi.common.ResponseBean;
import appapi.dao.ProjectMapper;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.CarInfo;
import appapi.entity.bridge.ParkCarInfo;
import appapi.mapper.ParkCarInfoMapper;
import appapi.mapper.CarMapper;
import appapi.service.ParkCarService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hlm
 * @date
 * @description: 闸机车辆
 */
@Service
public class ParkCarServiceImpl extends ServiceImpl<CarMapper, CarInfo> implements ParkCarService {

    @Resource
    private CarMapper carMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private ParkCarInfoMapper parkCarInfoMapper;

    /**
     * 分页查询闸机车辆列表
     *
     * @return
     */
    @Override
    public IPage<CarVO> getParkCarByPage(ParkCarPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<CarVO> carVOList = parkCarInfoMapper.getParkCarByPage(req);
        PageInfo<CarVO> info = new PageInfo<>(carVOList);
        IPage<CarVO> carVOIPage = new Page<>();
        if (carVOList.size() > 0) {
            carVOIPage.setRecords(carVOList);
            carVOIPage.setSize(req.getPageSize());
            carVOIPage.setTotal(info.getTotal());
            carVOIPage.setCurrent(req.getPageNum());
        }
        return carVOIPage;
    }

    /**
     * 注册
     *
     * @return
     */
    @Override
    public void insertCar(AddCar addCar, UserInfo userInfo) {
        //生成车辆信息
        CarInfo carInfo = BeanCopyUtils.copyBean(addCar, CarInfo.class);
        carInfo.setCreateTime(LocalDateTime.now());
        carInfo.setCreateUser(userInfo.getUserid());
        carInfo.setLastModifyUser(userInfo.getUserid());
        carInfo.setLastModifyTime(LocalDateTime.now());
        carInfo.setCarStatus(0);
        carMapper.insert(carInfo);
        //生成闸机车辆信息
        ParkCarInfo parkCarInfo = new ParkCarInfo();
        parkCarInfo.setCarId(carInfo.getCarId());
        parkCarInfo.setParkId(addCar.getParkId());
        parkCarInfo.setAuthFlag(1);
        parkCarInfo.setCreateTime(LocalDateTime.now());
        parkCarInfo.setCreateUser(userInfo.getUserid());
        parkCarInfo.setLastModifyUser(userInfo.getUserid());
        parkCarInfo.setLastModifyTime(LocalDateTime.now());
        parkCarInfoMapper.insert(parkCarInfo);
    }

    /**
     * 编辑
     *
     * @return
     */
    @Override
    public ResponseBean editCar(EditCar editCar, UserInfo userInfo) {
        CarInfo carInfo = BeanCopyUtils.copyBean(editCar, CarInfo.class);
        carInfo.setLastModifyTime(LocalDateTime.now());
        carInfo.setLastModifyUser(userInfo.getUserid());
        carMapper.updateById(carInfo);
        return ResponseBean.success();
    }

    /**
     * 删除
     *
     * @return
     */
    @Override
    public void delCar(Long carId) {
        //闸机车辆信息
        LambdaQueryWrapper<ParkCarInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkCarInfo::getCarId, carId);
        ParkCarInfo parkCarInfo = new ParkCarInfo();
        parkCarInfo.setAuthFlag(0);
        parkCarInfoMapper.update(parkCarInfo, wrapper);
        //车辆信息
        CarInfo carInfo = carMapper.selectById(carId);
        carInfo.setCarStatus(1);
        carMapper.updateById(carInfo);
    }

    /**
     * 详情
     *
     * @return
     */
    @Override
    public ResponseBean<CarVO> getParkModel(Long carId) {
        CarInfo carInfo = carMapper.selectById(carId);
        CarVO carVO = BeanCopyUtils.copyBean(carInfo, CarVO.class);
        //封装项目信息
        ProjectDto projectDto = projectMapper.selectByPrimaryKey(carVO.getProjId());
        if (projectDto != null) {
            carVO.setProjName(projectDto.getProjName());
        }
        return ResponseBean.success(carVO);
    }

}
